<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <a-alert  message="请先填写基本信息提交保存后，再上传证照扫描图片" type="warning" show-icon />
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '../common/basicModal'
import basicForm from '../common/basicForm'
import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getInfoByPid, saveInfo } from './license'

export default {
  components: {
    basicModal,
    basicForm,
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增品种',
        modalWidth: '800px',
      },
      s_FormInfo: [
        { label: '供应商', name: 'suoShupPid', type: 'supplier', placeholder: '请选择' },
        // { label: '证照类型', name: 'zhengZhaolx', type: 'dicselect',dicName:'zhengZhaolx', placeholder: '请选择' },
        { label: '证照类型', name: 'zhengZhaolx', type: 'dicselect',dicName:'zhengZhaolx', placeholder: '请选择' },
        { label: '证照号码', name: 'zhengZhaohm', type: 'input', placeholder: '请输入' },
        { label: '颁发机构', name: 'banFajg', type: 'input', placeholder: '请输入' },
        { label: ['有效期','到'], name: ['youXiaoqs','youXiaoqz'], type: 'datatimeR', placeholder: '请输入' },
        { label: '预警日期', name: 'yuJingrq', type: 'datatime', placeholder: '请输入' },
        { label: '状态', name: 'zhuangTai', type: 'dicselect',dicName:'licenseStatus',  placeholder: '请选择',options: [{ id: '1', value: '正常' },{ id: '2', value: '已过期' }] },
        { label: ['保存', '重置'], type: 'botton' },
      ],
      s_formDatas: {
        sysSeq: 0,
        pid: null,
        zhengZhaofl: null,
        zhengZhaolx: null,
        suoShupmc: null,
        suoShupdm: null,
        zhengZhaohm: null, 
        banFajg: null,
        youXiaoqs: null,
        youXiaoqz: null,
        yuJingrq: null,
        zhuangTai: null,
        suoShupPid: null
      },
      s_rules: {
        suoShupPid: [{ required: true, message: '客户必须选择', trigger: 'blur' }],
        zhengZhaolx: [{ required: true, message: '证照类型必填', trigger: 'blur' }],
        zhengZhaohm: [{ required: true, message: '证照号码必填', trigger: 'change' }],
        banFajg: [{ required: true, message: '颁发机构必填', trigger: 'change' }]
      },
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      this.s_formDatas = {
        sysSeq: 0,
        pid: null,
        zhengZhaofl: null,
        zhengZhaolx: null,
        suoShupmc: null,
        suoShupdm: null,
        zhengZhaohm: null, 
        banFajg: null,
        youXiaoqs: null,
        youXiaoqz: null,
        yuJingrq: null,
        zhuangTai: null,
        suoShupPid: null
      }
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增证照'
    },
    // 新增按钮 点击了新增
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改证照'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      const _this = this
      switch (name) {
        case 'xiaoShoucpPid':
          _this.getSaleGoodsInfo(val)
          break
        case 'keHuPid':
          _this.getCustomerInfo(val)
          break
        case 'zhongBiaolx':
          _this.s_formDatas.zhongBiaolx = val
          break
      }
    },
    // 获取供应商信息
    async getCustomerInfo(pid) {
      const _this = this
      const res = await getSupplierInfoByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        _this.s_formDatas.keHuPid = res.data.pid
        _this.s_formDatas.keHuOrgPid = res.data.keHuOrgPid
        _this.s_formDatas.keHudm = res.data.keHudm
        _this.s_formDatas.keHumc = res.data.keHumc
        
      }
    },
    //
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    },
  },
}
</script>

<style>
</style>